from threading import Thread
import threading
from time import sleep
import os

# a = 1
# def music():
#     global a
#     print("a = ", a)
#     a = 10000
#     for i in range(3):
#         sleep(2)
#         print(os.getpid(),"播放：黄河大合唱")
#
# thread = threading.Thread(target=music)
# thread.start()
#
# for i in range(10):
#     sleep(1)
#     print(os.getpid(),"播放：葫芦娃")
#
# thread.join()
# print("a = ", a)


# def func(sec, name):
#     print("含有参数的线程来咯")
#     sleep(sec)
#     print("%s 线程执行完毕"%name)
#
# for i in range(5):
#     t = Thread(target = func,
#                args = (2,),
#                kwargs = {"name":"T-%d"%i},
#                daemon = True)
#     t.start()
#     t.join()

# class MyThread(Thread):
#     def __init__(self, song):
#         self.song = song
#         super().__init__()
#
#     def run(self):
#         for i in range(3):
#             sleep(2)
#             print("播放：", self.song)
#
# t = MyThread("凉凉")
# t.start()

# from threading import Lock
# lock = Lock()
#
# tickets = ["T%d"%x for x in range(1,501)]
#
# def sell(w):
#     while tickets:
#         print(f"{w} selling {tickets.pop(0)}")
#         sleep(0.1)
# for i in range(1,11):
#     t = Thread(target=sell, args=(f"窗口{i}",))
#     t.start()
#     # t.join()

# from threading import Thread,Lock
#
# lock1=Lock()
# lock2=Lock()
# def print_numbers():
#     for i in range(1,53,2):
#         lock1.acquire()
#         print(i)
#         print(i+1)
#         lock2.release()
#         # time.sleep(0.1)
#
# def print_letters():
#     for i in range(65,91):
#         lock2.acquire()
#         print(chr(i))
#         lock1.release()
#         # time.sleep(0.1)
#
# thread1 = Thread(target=print_numbers)
# thread2 = Thread(target=print_letters)
#
# lock2.acquire()
#
# thread1.start()
# thread2.start()

# from socket import *
# from multiprocessing import Process
# import sys
#
# HOST = "0.0.0.0"
# PORT = 8888
# ADDR = (HOST, PORT)
#
# def handle(connfd):
#     while True:
#         data = connfd.recv(1024)
#         if not data:
#             break
#         print(data.decode())
#     connfd.close()
#
# def main():
#     tcp_socket = socket()
#     tcp_socket.bind(ADDR)
#     tcp_socket.listen(5)
#     print(f"Listen the port {PORT}")
#
#     while True:
#         try:
#             connfd, addr = tcp_socket.accept()
#             print("Connect from", addr)
#         except KeyboardInterrupt:
#             tcp_socket.close()
#             sys.exit("服务结束")
#
#         p = Process(target=handle, args=(connfd,), daemon=True)
#         p.start()
# if __name__ == '__main__':
#     main()

from socket import *
from threading import Thread

class Handle:
    def request(self,data):
        print(data)

class ThreadServer(Thread):
    def __init__(self,connfd):
        self.connfd = connfd
        self.handle = Handle()
        super().__init__(daemon=True)

    def run(self):
        while True:
            data = self.connfd.recv(1024).decode()
            if not data:
                break
            self.handle.request(data)
        self.connfd.close()

class ConcurrentServer:
    def __init__(self, *, host="", port=0):
        self.host = host
        self.port = port
        self.address = (host, port)
        self.sock = self.__create_socket()

    def __create_socket(self):
        tcp_socket = socket()
        tcp_socket.bind(self.address)
        return tcp_socket

    def serve_forever(self):
        self.sock.listen(5)
        print(f"Listen the port {self.port}")

        while True:
            connfd, addr = self.sock.accept()
            print("Connect from", addr)
            t = ThreadServer(connfd)
            t.start()

if __name__ == '__main__':
    server = ConcurrentServer(host="0.0.0.0", port=8888)
    server.serve_forever()